//////////////////////////////////////////////////////////////////////////
#include "Sprite.h"
#include "../Texture/Texture.h"
#include "../TextureManager/TextureManager.h"
#include <cassert>
#include "MainHelpers/MainHelpers.h"
//////////////////////////////////////////////////////////////////////////
extern HGE* hge;
//////////////////////////////////////////////////////////////////////////
Sprite::Sprite()
:m_anchorX(0)
,m_anchorY(0)
,m_texture(NULL)
{
	
}
//////////////////////////////////////////////////////////////////////////
Sprite::~Sprite()
{
	if(m_texture)
	{
		TextureManager::freeTexture(m_texture);
		m_texture = NULL;
	}
}
//////////////////////////////////////////////////////////////////////////
void Sprite::setAnchor( float x, float y )
{
	m_anchorX = x;
	m_anchorY = y;

	updateRenderQuad();
}
//////////////////////////////////////////////////////////////////////////
void Sprite::setTexturePath( const wchar_t* path )
{
	m_texture = TextureManager::loadTexture(path);

	m_renderQuad.tex = m_texture->hgeTexture();

	m_renderQuad.v[0].tx = 0.0f; m_renderQuad.v[0].ty = 0.0f;
	m_renderQuad.v[1].tx = 1.0f; m_renderQuad.v[1].ty = 0.0f;
	m_renderQuad.v[2].tx = 1.0f; m_renderQuad.v[2].ty = 1.0f;
	m_renderQuad.v[3].tx = 0.0f; m_renderQuad.v[3].ty = 1.0f;

	m_renderQuad.blend = BLEND_DEFAULT;
	
	for(int i = 0 ; i < 4; ++i)
	{
		m_renderQuad.v[i].col = 0xffffffff;
		m_renderQuad.v[i].z = 0.0f;
	}

	updateRenderQuad();
}
//////////////////////////////////////////////////////////////////////////
void Sprite::updateRenderQuad()
{
	if(!m_texture)
	{
		return;
	}
	
	int width = m_texture->getWidth();
	int height = m_texture->getHeight();
	m_renderQuad.v[0].x = -m_anchorX;		  m_renderQuad.v[0].y = -m_anchorY;
	m_renderQuad.v[1].x = -m_anchorX + width; m_renderQuad.v[1].y = -m_anchorY;
	m_renderQuad.v[2].x = -m_anchorX + width; m_renderQuad.v[2].y = -m_anchorY + height;
	m_renderQuad.v[3].x = -m_anchorX;		  m_renderQuad.v[3].y = -m_anchorY + height;

	hlp::Pointf newPoint[4];

	for(int i = 0; i < 4; i++)
	{
		hlp::Pointf oldPoint;
		oldPoint.x = m_renderQuad.v[i].x;
		oldPoint.y = m_renderQuad.v[i].y;
		newPoint[i] = hlp::transformPoint(oldPoint, m_transformationMatrix);
	}
	for(int i = 0; i < 4; i++)
	{
		m_renderQuad.v[i].x = newPoint[i].x;
		m_renderQuad.v[i].y = newPoint[i].y;
	}
}
//////////////////////////////////////////////////////////////////////////
void Sprite::updateTransformationMatrix()
{
	CompositeObject::updateTransformationMatrix();
	updateRenderQuad();
}
//////////////////////////////////////////////////////////////////////////
void Sprite::render()
{
	if(!m_visible)
		return;
	
	assert(m_texture);
	if(!m_texture) return;

	hge->Gfx_RenderQuad(&m_renderQuad);
	CompositeObject::render();
}
//////////////////////////////////////////////////////////////////////////
int Sprite::getWidth() const
{
	return m_texture->getWidth();
}
//////////////////////////////////////////////////////////////////////////
int Sprite::getHeight() const
{
	return m_texture->getHeight();
}
//////////////////////////////////////////////////////////////////////////
void Sprite::setCenterAlign()
{
	setAnchor(static_cast<float>(getWidth()/2), static_cast<float>(getHeight())/2);
}
//////////////////////////////////////////////////////////////////////////